<template>
	<view class="page">
		<!-- 顶部标题 -->
		<view class="header">
			<view class="header-content">
				<text class="title">派单中心</text>
			</view>
		</view>

		<!-- 订单列表 -->
		<scroll-view scroll-y class="order-list">
			<view v-for="(order, index) in orders" :key="index" class="order-card">
				<!-- 标题栏 -->
				<view class="order-header">
					<view>
						<text class="order-title">{{ order.title }}</text>
						<view class="order-subtitle">
							<text class="order-number">订单号: {{ order.orderno }}</text>
							<text v-if="order.isUrgent" class="urgent-badge">紧急</text>
						</view>
					</view>
					<text class="status-badge" :class="{
				            'status-available': order.status === 0,
				            'status-accepted': order.status === 1,
				            'status-ended': order.status === 2
				        }">{{
				            order.status === 0 ? '待接单' : 
				            order.status === 1 ? '已接单' : 
				            '已结束' 
				        }}</text>
				</view>

				<!-- 详情 -->
				<view class="order-details">
					<view class="detail-item">
						<text class="detail-label">项目经理</text>
						<text class="detail-value phone-link" >{{ order.pmId }}</text>
					</view>
					<view class="detail-item">
						<text class="detail-label">服务时间</text>
						<text class="detail-value">{{ formatTime(order.starttime) }} -
							{{ formatTime(order.endtime) }}</text>
					</view>
					<view class="detail-item">
						<text class="detail-label">服务地点</text>
						<text class="detail-value">{{ order.location }}</text>
					</view>
					<view class="detail-item">
						<text class="detail-label">预计报酬</text>
						<text class="detail-value reward">¥{{ order.reward }}</text>
					</view>
				</view>

				<!-- 需求 -->
				<view class="needs-box">
					<text class="detail-label">服务需求</text>
					<text class="detail-value needs-content">{{ order.requirements }}</text>
				</view>

				<!-- 任务要求，部分订单有 -->
				<view v-if="order.taskRequirements" class="needs-box" style="margin-top: 12rpx;">
					<text class="detail-label">任务要求</text>
					<text class="detail-value needs-content">{{ order.taskRequirements }}</text>
				</view>

				<!-- 接单按钮 -->
				<view class="btn-wrapper">
					<view v-if="order.status === 0" class="accept-btn" @click="acceptOrder(order)">立即接单</view>
					<view v-if="order.status === 1" class="btn-disabled" disabled>已接受</view>
					<view v-if="order.status === 2" class="btn-disabled" disabled>已完成</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		getJobList,
		confirmOrder
	} from "../../utils/getDate.js"
	export default {
		data() {
			return {
				orders: [],
			};
		},
		onShow() {
			this.ongetJobList()
		},
		methods: {
			formatTime(timeStr) {
				if (!timeStr) return ''
				// 简单格式化时间，去掉秒
				return timeStr.replace(/(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}):\d{2}/, '$1 $2')
			},
			acceptOrder(order) {
				uni.showModal({
					title: '接取订单',
					content: `前往接取 "${order.title}"`,
					success: (res) => {
						if (res.confirm) {
							// 跳转到确认订单页面，并传递订单信息
							uni.navigateTo({
								url: `/pages/TC/confirmOrder?order=${encodeURIComponent(JSON.stringify(order))}`
							});
						}
					},
				});
			},
			callPhone(phone) {
				uni.showModal({
					title: '拨打电话',
					content: `是否要拨打 ${phone}？`,
					success: (res) => {
						if (res.confirm) {
							uni.makePhoneCall({
								phoneNumber: phone
							});
						}
					},
				});
			},
			onSearch() {
				uni.showToast({
					title: '搜索功能未实现',
					icon: 'none'
				});
			},
			onFilter() {
				uni.showToast({
					title: '筛选功能未实现',
					icon: 'none'
				});
			},

			ongetJobList() {
				let data = {
					teacherId: uni.getStorageSync("teacher").id
				}
				getJobList(data).then(res => {
					if (res.state === 'ok') {
						// 对接口数据进行简单处理
						this.orders = res.data.map(item => ({
							...item,
							isUrgent: false // 可以根据需要从接口添加紧急状态
						}))
					}
				})
			}
		},
	};
</script>

<style scoped>
	/* 使用系统默认字体，确保兼容性 */
	.page {
		background-color: #f8fafc;
		color: #1e293b;
		font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", Helvetica, Arial, sans-serif;
		min-height: 100vh;
		display: flex;
		flex-direction: column;
	}

	/* 顶部标题栏 */
	.header {
		position: sticky;
		top: 0;
		z-index: 10;
		background: #fff;
		box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
	}

	.header-content {
		padding: 24rpx 32rpx;
		display: flex;
		align-items: center;
		box-sizing: border-box;
	}

	.title {
		font-size: 36rpx;
		font-weight: 600;
		color: #334155;
	}

	/* 订单列表 */
	.order-list {
		flex: 1;
		padding: 24rpx 32rpx;
		overflow-y: auto;
		background-color: #f8fafc;
		display: flex;
		flex-direction: column;
		gap: 32rpx;
		box-sizing: border-box;
	}

	/* 订单卡片 */
	.order-card {
		background-color: #fff;
		border-radius: 24rpx;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05), 0 2rpx 6rpx rgba(0, 0, 0, 0.03);
		overflow: hidden;
		display: flex;
		flex-direction: column;
		margin-bottom: 32rpx;
	}

	/* 标题栏 */
	.order-header {
		padding: 32rpx;
		border-bottom: 1rpx solid #f1f5f9;
		display: flex;
		justify-content: space-between;
		align-items: flex-start;
	}

	.order-title {
		font-size: 32rpx;
		font-weight: 600;
		color: #334155;
		line-height: 1.4;
	}

	.order-subtitle {
		margin-top: 12rpx;
		display: flex;
		align-items: center;
		gap: 16rpx;
		flex-wrap: wrap;
	}

	.order-number {
		font-size: 24rpx;
		color: #64748b;
	}

	.urgent-badge {
		font-size: 24rpx;
		background-color: #fef2f2;
		color: #dc2626;
		padding: 4rpx 16rpx;
		border-radius: 12rpx;
	}

	.status-badge {
		font-size: 26rpx;
		padding: 8rpx 24rpx;
		border-radius: 9999rpx;
		font-weight: 600;
		white-space: nowrap;
		user-select: none;
		margin-left: 16rpx;
	}

	.status-available {
		background-color: #eff6ff;
		color: #2563eb;
	}

	.status-accepted {
		background-color: #d1fae5;
		color: #059669;
	}

	.status-ended {
		background-color: #f1f5f9;
		color: #64748b;
	}

	/* 订单详情 */
	.order-details {
		padding: 24rpx 32rpx;
		display: grid;
		grid-template-columns: 1fr 1fr;
		gap: 24rpx 32rpx;
	}

	.detail-item {
		display: flex;
		flex-direction: column;
	}

	.detail-label {
		font-size: 26rpx;
		color: #64748b;
		font-weight: 500;
	}

	.detail-value {
		margin-top: 8rpx;
		font-size: 28rpx;
		color: #1e293b;
		line-height: 1.4;
		word-break: break-all;
	}

	.reward {
		color: #2563eb;
		font-weight: 600;
	}

	/* 服务需求和任务要求框 */
	.needs-box {
		background-color: #f1f5f9;
		border-left: 8rpx solid #0ea5e9;
		padding: 24rpx;
		margin: 0 32rpx 24rpx 32rpx;
		border-radius: 12rpx;
	}

	.needs-content {
		margin-top: 16rpx;
		font-size: 28rpx;
		color: #1e293b;
		line-height: 1.5;
	}

	/* 按钮区域 */
	.btn-wrapper {
		padding: 0 32rpx 32rpx 32rpx;
	}

	.accept-btn {
		text-align: center;
		background-color: #2563eb;
		color: #fff;
		font-size: 30rpx;
		padding: 15rpx 10rpx;
		border-radius: 48rpx;
		font-weight: 600;
		box-shadow: 0 8rpx 16rpx rgba(37, 99, 235, 0.2);
		transition: all 0.2s ease;
		line-height: 1.5;
		width: 200rpx;
		margin-left: 360rpx;
	}

	.accept-btn:active {
		background-color: #1d4ed8;
		transform: scale(0.98);
	}

	.btn-disabled {
		text-align: center;
		background-color: #e2e8f0;
		color: #94a3b8;
		font-size: 30rpx;
		padding: 15rpx 10rpx;
		border-radius: 48rpx;
		font-weight: 600;
		box-shadow: 0 8rpx 16rpx rgba(37, 99, 235, 0.2);
		transition: all 0.2s ease;
		line-height: 1.5;
		width: 200rpx;
		margin-left: 360rpx;
	}

	/* 电话号码点击样式 */
	.phone-link {
		color: #2563eb;
		text-decoration: none;
	}

	.phone-link:active {
		opacity: 0.8;
	}

	/* 响应式调整 */
	@media (max-width: 360px) {

		.header-content,
		.order-list {
			padding-left: 24rpx;
			padding-right: 24rpx;
		}

		.order-header,
		.order-details {
			padding: 24rpx;
		}

		.needs-box {
			margin-left: 24rpx;
			margin-right: 24rpx;
		}
	}
</style>